<%@ page import="org.grails.plugins.lookups.Lookup" %>
<%@ page import="org.ibiblio.terasaur.search.DateHelper" %>
<%

def deleteLinkAction
def deleteLinkParams
def deleteOnclick
def showEditButton
if (itemInstance.deleted) {
    deleteLinkAction = 'undelete'
    deleteLinkParams = [deleted:'0', version: itemInstance.version]
    deleteOnclick = ''
    showEditButton = false
} else {
    deleteLinkAction = 'delete'
    deleteLinkParams = [deleted:'1', version: itemInstance.version]
    deleteOnclick = 'return confirm(\'' + message(code:'default.button.delete.confirm.message') + '\');'
    showEditButton = true
}

%>
<g:if test="${canEditItem}">
    <r:script disposition='head'>
       jQuery(function(){
            function toggleAddTorrentButton() {
                $( "#add-download-button" ).toggle();
            }

            // callback function to bring a hidden box back
            function callback() {
                setTimeout(function() {
                    $( "#add-download:visible" ).removeAttr( "style" ).fadeOut();
                }, 1000 );
            };

            $( "#add-download-button" ).click(function() {
                $( "#add-download" ).slideDown();
                toggleAddTorrentButton();
                return false;
            });

            $( "#add-download-cancel-button" ).click(function() {
                $( "#add-download" ).slideUp();
                toggleAddTorrentButton();
                return false;
            });

            $( "#add-download" ).hide();
        });
    </r:script>
</g:if>
        <div class="body hentry">
            <h2 class="entry-title full-title">${itemInstance.title}
                (<%= Lookup.valueForCode("item.type", itemInstance.type) %>)</h2>
<g:if test="${itemInstance.deleted}">
            <div class="message">${message(code: 'item.status.deleted.message')}</div>
</g:if>
            <div id="item-details" class="full full-content">
                <div class="full-content body-section">${itemInstance.description}</div>
                <div class="clear"></div>
<g:if test="${canEditItem}">
                <div class="edit delete-item">
                    <g:link action="update" id="${itemInstance.id}" params="${deleteLinkParams}"
                        onclick="${deleteOnclick}" class="edit-button"
                        title="${message(code:'item.' + deleteLinkAction + '.button.title')}">${message(code:'item.' + deleteLinkAction + '.button.text')}</g:link>
                </div>
    <g:if test="${showEditButton}">
                <div class="edit edit-item">
                    <g:link mapping="generic" action="edit" id="${itemInstance.id}" class="edit-button"
                        title="${message(code: 'item.edit.button.title')}">${message(code: 'item.edit.button.text')}</g:link>
                </div>
    </g:if>
</g:if>
            </div><!-- item-details -->

            <div class="clear"></div>
            <div class="rule"><hr /></div>
        </div>

        <div class="body hentry">
<g:if test="${canEditItem}">
            <div id="add-download">
                <div class="body-section">
                    <span>Add download</span> <img id="add-download-cancel-button" src="/images/icons/dialog-close.png" title="Cancel" alt="Cancel" />
                </div>
                <g:form method="post" enctype="multipart/form-data" url="[controller:'torrent',action:'add']" >
                    <div>
                    <input type="hidden" name="id" value="${itemInstance.id}" />
                    <input type="file" name="torrentFile" size="70" />
                    <input type="submit" value="Upload file" />
                    </div>
                </g:form>

                <h4>Approved trackers</h4>
                <ul>
                    <g:each in="${grailsApplication.config.terasaur.torrent.approved_trackers}" var="url" >
                    <li>${url}</li>
                    </g:each>
                </ul>
                <div class="clear"></div>
                <div class="rule"><hr /></div>
            </div>
</g:if>

            <div id="item-downloads" class="full full-content">
                <div class="body-section">
<g:if test="${downloadsList.totalCount > 0}">
                    <span class="body-section"><ts:itemLink action="downloads" item="${itemInstance}">Downloads</ts:itemLink> (${downloadsList.totalCount} total)</span>
</g:if>
<g:else>
                    <span class="body-section">Downloads</span>
</g:else>
<g:if test="${canEditItem}">
                    <img id="add-download-button" src="${resource(dir:'/images/silk',file:'add.png')}" title="Add download" alt="Add download" />
</g:if>
                </div>
<% if (downloadsList.totalCount > 0) { %>
                <ul class="item-downloads">
<g:each in="${downloadsList}" var="download">
                    <li class="item-downloads">
                        <ts:downloadLink torrent="${download}"><img src="/images/icons/download_16x16.png" title="Download torrent" alt="Download torrent" /></ts:downloadLink>
                        <ts:torrentLink torrent="${download}">${download.name.encodeAsHTML()}</ts:torrentLink>
                        (<ts:formatFileSize file_size="${download.size}" precision="2"/>)
    <g:if test="${canEditItem}">
                        <g:link controller="torrent" action="delete" id="${download.infoHash}" title="Delete download"
                        onclick="return confirm('${message(code: 'torrent.delete.confirm.message')}');" ><img src="${fam.icon(name: 'delete')}" alt="delete"/></g:link>
    </g:if>
                    </li>
</g:each>
                </ul>
                <div class="body-section">
    <g:if test="${downloadsList.totalCount > downloadsList.size()}">
                   <ts:itemLink action="downloads" item="${itemInstance}">more downloads...</ts:itemLink>
    </g:if>
                </div>
<% } else { %>
                ${message(code: 'item.status.no_downloads.message')}
<% } // if downloadsList.totalCount > 0 %>
            </div>
            <div class="full full-content">
                <div class="clear"></div>
                <div class="attribute left">
                    <span class="by alt-font">Published by</span> ${itemInstance.owner.username}
                    <span class="date full-date"> <span class="ndash alt-font">&ndash;</span> <joda:format value="${itemInstance.published}" style="L-" /></span>
<g:if test="${canEditItem}">
                    <span>
                        <g:link mapping="generic" action="update" id="${itemInstance.id}" title="${message(code: 'item.' + setPublishedLabel + '.button.title')}"
                            params="[version: itemInstance.version, published:setPublishedDate]"
                            onclick="${setPublishedOnclick}">${message(code: 'item.' + setPublishedLabel + '.button.text')}</g:link>
                    </span>
</g:if>
                </div>
                <div class="attribute right">
                    <span class="alt-font">Rights</span>
                    <%= Lookup.valueForCode("item.license", itemInstance.license) %>
                </div>
                <div class="clear"></div>
                <div class="attribute left">
                    <span class="alt-font">Collection</span> <span class="ndash alt-font">&ndash;</span> <a href="${collectionBrowseLink}">${itemInstance.collection.name}</a>
                </div>
            </div>
            <div class="clear"></div>
            <div class="rule"><hr /></div>

        </div><!-- .body -->
